package relu.R.birds;
import relu.R.landscape.*;
import relu.R.weeds.*;
import java.util.*;
import jfm.utils.*;
public class Bird {
	public double xHome;
	public double yHome;
	public final BirdSpecies species;
	// Make this a set to avoid double counting
	private Set<Field> accessibleFields=new HashSet<Field>();
	
	public void addField(Field f){
		accessibleFields.add(f);
	}
	public boolean inRange(double x,double y,double sideLen){
	/*	double dx=Landscape.getMinimumImageDistance(xHome,x,sideLen);
		double dy=Landscape.getMinimumImageDistance(yHome, y, sideLen);
//		double dy=yHome-y;
		
		double dist=Math.sqrt(dx*dx+dy*dy);
		if ( (dist-species.homeRange)<0){
			return true;
		} else {
			return false;
		}*/
		return true;
	}
	
	public Set<Bird> breed(LSFarm farm,double sideLength){
		Set<Bird> young=new HashSet<Bird>();
		int clutch=(int)(Landscape.rGen.nextGaussian()*species.varClutchSize+species.meanClutchSize);
		clutch=Math.max(0, clutch);
		// At present dispersal completely random into landscape
		double xn=Landscape.rGen.nextDouble()*sideLength;
		double yn=Landscape.rGen.nextDouble()*sideLength;
		for(int i=0;i<clutch;i++){
			young.add(new Bird(species,xn,yn));
		}
		
		return young;
	}
	
	public double availableShare(){
		double avail=0;
//		System.out.println("This birds has access to "+accessibleFields.size());
		Iterator<Field> it=accessibleFields.iterator();
		while(it.hasNext()){
			Field theField=it.next();
			for( WeedSpecies wsp:species.diet){
				avail+=theField.area()*species.foodFromWeedSpecies(theField.getShare(wsp),wsp);
			}
		}
		return avail;
	}
	
	public boolean enoughFood(){
		double avail=availableShare();
//		System.out.println("This birds has access to "+accessibleFields.size());
		if ( avail < species.dailyRation*SpatialLandscape.nWinterDays){
			return false;
		} else {
			return true;
		}
	}
	
	public Bird(BirdSpecies sp,double xh,double yh){
		species=sp;
		xHome=xh;
		yHome=yh;
	}
}
